home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 205_01 / entab.c < prev    next >
Text File  |  1980-01-01  |  3KB  |  113 lines

  1. /*
  2. HEADER:                 CUG205.00;
  3. TITLE:                  ENTAB for Microsoft C;
  4. DATE:                   09/24/86;
  5. DESCRIPTION:
  6.   "Places TABs at the appropriate locations in text files.";
  7. KEYWORDS:               Software tools, Text filters;
  8. SYSTEM:                 MS-DOS;
  9. FILENAME:               ENTAB.C;
  10. WARNINGS:
  11.   "The author claims copyrights and authorizes non-commercial use only.";
  12. AUTHORS:                 Michael M. Yokoyama;
  13. COMPILERS:              Microsoft;
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. #define MAXLINE    100          /* Maximum line size            */
  20. #define TABINC       8          /* Default TAB increment size   */
  21. #define YES          1
  22. #define NO           0
  23.  
  24. FILE *in;                       /* File used for input            */
  25.  
  26. main(argc,argv)
  27. int argc;                       /* Number of command line words   */
  28. char *argv[];                   /* Pointers to command line words */
  29. {
  30.   int tab[MAXLINE+1];
  31.  
  32.   if (argc != 2) {
  33.     fprintf(stderr,"TAB placement utility\n");
  34.     fprintf(stderr,"Usage:      entab filename\n");
  35.     exit(1);
  36.   }
  37.  
  38.   if ((in = fopen(argv[1],"r")) == NULL ) {
  39.     fprintf(stderr,"entab:  can't open %s\n",argv[1]);  
  40.     exit(1);
  41.   }
  42.  
  43.   settab(argc,TABINC,tab);
  44.   entab(tab);
  45.   fclose(in);
  46. }
  47.  
  48. /* Initialize TAB stops
  49. */
  50. settab(argc, argv, tab)
  51. int argc;
  52. char *argv[];
  53. int tab[];
  54. {
  55.   int i;
  56.  
  57.   for (i = 1; i <= MAXLINE ; i++)
  58.     if (i %     TABINC == 1)
  59.       tab[i] = YES;
  60.   else
  61.     tab[i] = NO;
  62. }
  63.  
  64. /* Insert TABs
  65. */
  66. entab(tab)
  67. int tab[];
  68. {
  69.   int c, pos;
  70.   int nb = 0;                   /* Number of blanks needed      */
  71.   int nt = 0;                   /* Number of TABs needed        */
  72.  
  73.   for (pos = 1; (c = getc(in)) != EOF; pos++)
  74.     if (c == ' ')
  75.       if (tabpos(pos+1, tab) == NO)
  76.         ++nb;                   /* Insert blank         */
  77.       else {
  78.         nb = 0;                 /* Insert TAB           */
  79.         ++nt;
  80.       }
  81.     else {
  82.       while (nt) {              /* Output               */
  83.         putchar('\t');
  84.         --nt;
  85.       }
  86.       if (c!=   '\t')
  87.         while (nb > 0) {
  88.           putchar(' ');
  89.           --nb;
  90.         }
  91.       else
  92.         nb = 0;
  93.       putchar(c);
  94.       if (c == '\n')
  95.         pos = 0;
  96.       else if   (c == '\t')
  97.         while (tabpos(pos+1,tab) != YES)
  98.           ++pos;
  99.     }
  100. }
  101.  
  102. /* Determine if position is a TAB stop
  103. */
  104. tabpos(pos, tab)
  105. int pos;
  106. int tab[];
  107. {
  108.   if (pos       > MAXLINE)
  109.     return(YES);
  110.   else
  111.     return(tab[pos]);
  112. }
  113.